import json
import sys
from pathlib import Path
from os.path import dirname

path = dirname(str(Path(__file__).resolve().parents[2]))
if path not in sys.path:
    sys.path.append(path)

from xkw_data_query.bapi.bapi_call import BapiCaller

from xkw_data_query.config_parse import get_bapi_config

# todo fixme zhangli需要优化，统一配置
config = get_bapi_config("rbm")
caller = BapiCaller(True, config["app_id"], config["app_secret"])


def fetch_download_url_by_resource_ids(resource_ids: list):
    """
    获取资源下载链接
    返回值:
    [
        "https://xkw-rbm-dev.oss-cn-hangzhou.aliyuncs.com/xkw-rbm-dev/resource/111/111.zip",
        "https://xkw-rbm-dev.oss-cn-hangzhou.aliyuncs.com/xkw-rbm-dev/resource/222/222.zip"
    ]
    """
    ret = []
    for resource_id in resource_ids:
        res = caller.get(f"/resources/{resource_id}/file-download-url")
        if res is None:
            return []
        if res.startswith("\"") and res.endswith("\""):
            res = res[1:-1]
        ret.append(res)
    return ret

def get_resource_info(resource_ids: list):
    """
    获取资源信息
    """
    ret = []
    for resource_id in resource_ids:
        res = caller.get(f"/resources/{resource_id}")
        if res is None:
            return []
        ret.append(json.loads(res))
    return ret
if __name__ == "__main__":
    res = get_resource_info([51691219])
    print(res)

    